/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author LiziTa
 */
public class CargaMasivaProdxAlmacen extends javax.swing.JPanel {

    /**
     * Creates new form CargaMasivaRecetas
     */
    public CargaMasivaProdxAlmacen() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BtnExaminar = new javax.swing.JButton();
        jProgressBar2 = new javax.swing.JProgressBar();
        TxtRuta = new javax.swing.JTextField();

        BtnExaminar.setText("Examinar");
        BtnExaminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnExaminarActionPerformed(evt);
            }
        });

        TxtRuta.setText("Ruta del archivo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(BtnExaminar, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(TxtRuta, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 65, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(BtnExaminar)
                    .addGap(0, 0, 0)
                    .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, 0)
                    .addComponent(TxtRuta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BtnExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnExaminarActionPerformed
        // TODO add your handling code here:
        JFileChooser dlg = new JFileChooser();
        int option = dlg.showOpenDialog(this);
        String extension = new String();
        if(option == JFileChooser.APPROVE_OPTION){
            File archivo = dlg.getSelectedFile();

            String file = dlg.getSelectedFile().getPath();

            extension =file.substring(file.lastIndexOf("."));

            if(extension.equals(".csv")) {
                try {
                    JOptionPane.showMessageDialog(this, "Presione aceptar para iniciar la carga, esto puede tomar varios minutos");
                    Controlador.Service.Almacen.GestionAlmacenes.cargaMasivaProdxAlmacen(file);
                    jProgressBar2.setValue(100);
                    JOptionPane.showMessageDialog(this, "La carga se realizo de manera correcta");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "El archivo no está en el formato adecuado");
                }
            }else{
                JOptionPane.showMessageDialog(this, "La extencion del Archivo debe ser CSV");

            }
        }
    }//GEN-LAST:event_BtnExaminarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnExaminar;
    private javax.swing.JTextField TxtRuta;
    private javax.swing.JProgressBar jProgressBar2;
    // End of variables declaration//GEN-END:variables
}
